﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using iMapa.UI.ModelView.Main;
using iMapa.UI.ModelView.Editor;
using System.ComponentModel;
using System.Collections.Specialized;
using iMapa.UI.View.Common;
using iMapa.UI.ModelView.Common;
using ActionPluginAPI;
using System.Collections.ObjectModel;
using iMapa.Backend;

namespace iMapa.UI.View.Editor
{
    /// <summary>
    /// Interaction logic for EditorContext.xaml
    /// </summary>
    public partial class EditorContent : UserControl
    {
        public EditorContent()
        {
            InitializeComponent();
        }

        private void UpdatePoints(object sender, NotifyCollectionChangedEventArgs e)
        {
            ObservableCollection<InteractObjectViewModel> model = (ObservableCollection<InteractObjectViewModel>)sender;
            canvas.Children.Clear();
            foreach (InteractObjectViewModel io in model)
            {
                canvas.Children.Add(io.CreatePoint());
            }

        }

        private void DeletePointButton_Click(object sender, RoutedEventArgs e)
        {
            MapViewModel model = (MapViewModel)DataContext;
            Button button = (Button)sender;
            InteractObjectViewModel iovm = (InteractObjectViewModel)button.DataContext;
            model.RemovePoint(iovm);
            if (iovm == model.SelectedIObj)
            {
                model.SelectedIObj = null;
            }
        }

        private void iPoints_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MapViewModel model=(MapViewModel)DataContext;
            if (iPoints.SelectedItem != null)
            {
                model.SelectedIObj = (InteractObjectViewModel)iPoints.SelectedItem;
            }

            UpdateParamsTabContent(iPoints.SelectedItem);
        }

        

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            MapViewModel model = (MapViewModel)DataContext;
            model.IObjectVMs.CollectionChanged += UpdatePoints;
            UpdatePoints(model.IObjectVMs, null);
        
        }
        private void selectAllTextInit(object sender, EventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox != null)
                textBox.SelectAll();
        }

        private void inputName_GotFocus(object sender, RoutedEventArgs e)
        {
            selectAllTextInit(sender, e);
        }

        private void inputName_GotMouseCapture(object sender, MouseEventArgs e)
        {
            selectAllTextInit(sender, e);
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MapViewModel model = (MapViewModel)DataContext;
            if (model != null)
            {
                UpdateParamsTabContent(model.SelectedIObj);
            }
        }

        private void UpdateParamsTabContent(object o)
        {
            InteractObjectViewModel io = (InteractObjectViewModel)iPoints.SelectedItem;
            if (io != null)
            {
                ActionTypeViewModel at = io.ActionType;
                if (at != null)
                {
                    if (io.IObject.ActionParams == null)
                    {
                        io.IObject.ActionParams = at.CreateParameterInstance(InteractMapLoader.GetInteractMapPath(io.Parent.Name));
                    }
                    actionParams.Child = at.CreateConfigurationPanel(io.IObject.ActionParams);
                }
                else
                {
                    actionParams.Child = null;
                }
            }
            else
            {
                actionParams.Child = null;
            }
        }

        private void mapa_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            MapViewModel model = (MapViewModel)DataContext;
            model.DisplaySize = new Size(mapa.ActualWidth, mapa.ActualHeight);
        }

        private void mapa_Loaded(object sender, RoutedEventArgs e)
        {
            MapViewModel model = (MapViewModel)DataContext;
            model.DisplaySize = new Size(mapa.ActualWidth, mapa.ActualHeight);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            MainViewModel.SetTooltip(iMapa.Properties.Resources.EditorTooltipHeader, iMapa.Properties.Resources.EditorTooltipText);
        }
    }
}
